package cn.px.service.monthly.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.PageHelper;
import cn.px.common.util.BigDecimalUtil;
import cn.px.common.util.IdGeneratorUtils;
import cn.px.dao.bean.MyVOPage;
import cn.px.dao.dto.monthly.MonthlySbThreeRateDTO;
import cn.px.dao.dto.repair.RepairFormDTO;
import cn.px.dao.entity.monthly.MonthlySbThreeRate;
import cn.px.dao.enums.SbInfoStatusEnum;
import cn.px.dao.mapper.monthly.MonthlySbThreeRateMapper;
import cn.px.dao.util.CalculateUtil;
import cn.px.dao.vo.query.monthly.MonthlySbThreeRateVO;
import cn.px.service.base.impl.BaseServiceImpl;
import cn.px.service.monthly.MonthlySbThreeRateService;
import cn.px.service.repair.RepairFormService;
import cn.px.service.sb.SbInfoService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.weekend.Weekend;
import tk.mybatis.mapper.weekend.WeekendCriteria;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Description 设备三率按月统计 service 实现类
 * @author 品讯科技
 * @date 2024-08
 * @Version Copyright (c) 2020,北京乾元坤和科技有限公司 All rights reserved.
 */
@AllArgsConstructor
@Service("monthlySbThreeRateService")
public class MonthlySbThreeRateServiceImpl extends BaseServiceImpl<MonthlySbThreeRateMapper, MonthlySbThreeRate, MonthlySbThreeRateDTO> implements MonthlySbThreeRateService {

    private final SbInfoService sbInfoService;

    private final RepairFormService repairFormService;

    @Override
    public int batchDelete(List<String> ids) {
        Weekend<MonthlySbThreeRate> weekend = new Weekend<>(MonthlySbThreeRate.class);
        WeekendCriteria<MonthlySbThreeRate, Object> weekendCriteria = weekend.weekendCriteria();
        weekendCriteria.andIn(MonthlySbThreeRate::getId, ids);
        mapper.deleteByExample(weekend);
        return 1;
    }

    @Override
    public List<MonthlySbThreeRateVO> selectVOList(MonthlySbThreeRateDTO record) {
        List<MonthlySbThreeRateVO> monthlySbThreeRateVOS = mapper.selectList(record);
        monthlySbThreeRateVOS.forEach(item -> {
            item.setIntactRate(CalculateUtil.calculateRatio(
                    BigDecimalUtil.valueOf(item.getIntactNum()), BigDecimalUtil.valueOf(item.getSumNum())));
            item.setRepairRate(CalculateUtil.calculateRatio(
                    BigDecimalUtil.valueOf(item.getRepairNum()), BigDecimalUtil.valueOf(item.getSumNum())));

            item.setAccidentRate(CalculateUtil.calculateRatio(
                    BigDecimalUtil.valueOf(item.getAccidentNum()), BigDecimalUtil.valueOf(item.getSumNum())));

        });
        return monthlySbThreeRateVOS;
    }

    @Override
    public MyVOPage<MonthlySbThreeRateVO> selectVOPage(MonthlySbThreeRateDTO record, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return new MyVOPage<>(this.selectVOList(record));
    }

    @Override
    public void getThreeRateNow() {
        int intactNum = sbInfoService.countByStatus(SbInfoStatusEnum.IN_USE.getValue());
        int repairNum = sbInfoService.countByStatus(SbInfoStatusEnum.IN_STOP.getValue());
        LocalDateTime now = LocalDateTime.now();
        int month = now.getMonthValue();
        int year = now.getYear();
        int accidentNum = repairFormService.countDicitinctSbByTime(new RepairFormDTO()
                .setSearchStartTime(LocalDateTime.of(year, month, 1, 0, 0, 0))
                .setSearchEndTime(now));
        int sumNum = sbInfoService.countByStatus();
        MonthlySbThreeRate entity = new MonthlySbThreeRate();
        entity.setIntactNum(intactNum);
        entity.setRepairNum(repairNum);
        entity.setAccidentNum(accidentNum);
        entity.setSumNum(sumNum);
        entity.setCreatedTime(now);
        entity.setUpdateTime(now);
        entity.setYear(year);
        entity.setMonth(month);
        entity.setId(IdGeneratorUtils.getObjectId());
        Weekend<MonthlySbThreeRate> weekend = new Weekend<>(MonthlySbThreeRate.class);
        WeekendCriteria<MonthlySbThreeRate, Object> weekendCriteria = weekend.weekendCriteria();
        weekendCriteria.andEqualTo(MonthlySbThreeRate::getYear, year);
        weekendCriteria.andEqualTo(MonthlySbThreeRate::getMonth, month);
        List<MonthlySbThreeRate> monthlySbThreeRates = mapper.selectByExample(weekend);
        if (CollectionUtil.isEmpty(monthlySbThreeRates)) {
            mapper.insertSelective(entity);
        } else {
            entity.setId(monthlySbThreeRates.get(0).getId());
            mapper.updateByPrimaryKeySelective(entity);
        }

    }

}
